package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

@RestController
@Slf4j
public class RabbitController {


    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * convertAndSend方法中我们使用到的参数:
     *      String exchange,
     *      String routingKey,
     *      final Object object, 要发送的对象
     *      @Nullable CorrelationData correlationData 该消息的唯一id，在消息确认中可以收到
     * @param num
     * @return
     */
    @ResponseBody
    @RequestMapping("/sendMq")
    public String sendMq(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0; i < 10; i++) {
//            if(i%2==0){
                //1、发送消息，如果发送的消息是个对象，我们会使用序列化机制，将对象写出去。类必须实现Serializable
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("哈哈"+i);
                //2、发送的对象类型的消息，可以转成一个json
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",reasonEntity,new CorrelationData(UUID.randomUUID().toString()));
                log.info("消息发送完成{}",reasonEntity);
            }
//        }
        return "ok";
    }
}
